// pages/tuihuo/tuihuo.js

var api = require("../../utils/api");
var cf = require("../../config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    imagesList: [
      '../../images/icon_A.png'
    ],
    imagesLists:[],
    show: true,
    //图片数组
    arrImgs: [],
    //评论内容
    content: '',
    amount: '',
    goodsDetailsId: '',
    goodsName: '',
    goodsPic: '',
    orderCode: '',
    orderDetailsId: '',
    specDesc: '',
    actualPrice:'',
    price:'',
    total:'',
    exchange:"",
    exchangePrice:"",
    exchangeNum:"",
    exchangePrice1:""
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.currentTarget.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.current
      })
    }
  },

  onLoad: function (options) {
     let that=this
      that.setData({
        amount: Number(options.amount),
        goodsDetailsId:options.goodsDetailsId,
        goodsName:options.goodsName,
        goodsPic:options.goodsPic,
        orderCode: options.orderCode,
        orderDetailsId: options.orderDetailsId,
        specDesc: options.specDesc,
        actualPrice: Number(options.actualPrice).toFixed(2),
        price: Number(options.price).toFixed(2),
        total: Number(options.price * options.amount).toFixed(2),
        exchange: '请输入小于或等于' + Number(options.amount)+'件商品',
        exchangePrice: '请输入小于或等于' + Number(options.price * options.amount).toFixed(2)+'金额',
        exchangeNum: '请输入小于或等于'+ Number(options.amount)+ '件商品',
        exchangePrice1: '请输入小于或等于' + Number(options.price * options.amount).toFixed(2) + '金额',
   
      })
  },
   
  //上传图片
  addImg1: function () {
    let that = this
    wx.chooseImage({
      count: 3,
      success(res) {
        const tempFiles = res.tempFiles
        if(tempFiles.some(img => img.size > 5000000)) {
          return wx.showToast({
            title: '上传图片不能大于5M',
            icon: "none",
          })
        }
        if (res.errMsg == "chooseImage:ok") {
          that.setData({
            imagesList: res.tempFilePaths,
            imagesLists: res.tempFilePaths,
            show: false,
            arrImgs:[]
          })
          //调用上传图片 url 数组图片
          that.uploadFiles(`${cf.url}/admin/ossUploading/image`, res.tempFilePaths)
        }
      }
    })
  },
  //上传图片
  addImg2: function () {
    let that = this
    wx.chooseImage({
      count: 3,
      success(res) {  
        const tempFiles = res.tempFiles
        if(tempFiles.some(img => img.size > 5000000)) {
          return wx.showToast({
            title: '上传图片不能大于5M',
            icon: "none",
          })
        }
        if (res.errMsg == "chooseImage:ok") {
          that.setData({
            imagesList: res.tempFilePaths,
            imagesLists: res.tempFilePaths,
            show: false,
            arrImgs: []
          })
          //调用上传图片 url 数组图片
          that.uploadFiles(`${cf.url}/admin/ossUploading/image`, res.tempFilePaths)
        }
      }
    })
  },
  //上传图片
  addImg3: function () {
    let that = this
    wx.chooseImage({
      count: 3,
      success(res) {  
        const tempFiles = res.tempFiles
        if(tempFiles.some(img => img.size > 5000000)) {
          return wx.showToast({
            title: '上传图片不能大于5M',
            icon: "none",
          })
        }
        if (res.errMsg == "chooseImage:ok") {
          that.setData({
            imagesList: res.tempFilePaths,
            imagesLists: res.tempFilePaths,
            show: false,
            arrImgs: []
          })
          //调用上传图片 url 数组图片
          that.uploadFiles(`${cf.url}/admin/ossUploading/image`, res.tempFilePaths)
        }
      }
    })
  },

  //删除图片
  deleteImages: function (e) {
    let that = this
    if (that.data.imagesList.length == 1) {
      that.setData({
        imagesList: ['../../images/icon_A.png'],
        show: true
      })
    } else {
      that.setData({
        imagesList: that.data.imagesList.splice(0, e.currentTarget.dataset.index),
        imagesLists: that.data.imagesLists.splice(0, e.currentTarget.dataset.index)
      })
      if (that.data.imagesList.length == 0) {
        that.setData({
          imagesList: ['../../images/icon_A.png'],
          show: true
        })
      }
    }
  },

  //图片上传服务器
  uploadFiles: function (url, arrImg) {
    let that = this;
    let arrImgs = that.data.arrImgs
    let num = 0
    //遍历数组每张图片
    arrImg.forEach(function (v) {
      wx.uploadFile({
        url: url,
        filePath: v,
        name: 'file',
        formData:{
          path:'image'
        },
        success(res) {
          res = JSON.parse(res.data) 
          if (res.result) {
            // splice
            arrImgs: arrImgs.push({
              url: res.data,
              fileType:1,
              orders: ++num
            })
          } else {
            wx.showToast({
              title: '上传失败',
              icon: "none",
            })
          }
        }
      })
    })
    that.setData({
      arrImgs: arrImgs
    })
  },
  

  //提交
  handleSubmit1:function(e){
    let that=this
    //选择的是退货 退款 
    let currentTab = that.data.currentTab
    //退货数量
    let returnNum=""
    //退货金额
    let returnAmount=""

    let myreg = /(^[1-9]\d*$)/;

 
      wx.showLoading({
          title: '加载中...'
      })

    if (currentTab==0){
      if (e.detail.value.goodsNum==""){
        wx.showToast({
          title: '请输入退货数量',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      //如果输入的商品数量大于 真实的数量 提示
      if (Number(e.detail.value.goodsNum) > Number(that.data.amount) ) {
        wx.showToast({
          title: '请输入正确的数量',
          icon: 'none',
          duration: 2000
        })
        return false
      }

      if (!myreg.test(e.detail.value.goodsNum)) {
        wx.showToast({
          title: '请输入正确的数量',
          icon: 'none',
          duration: 2000
        })
        return false
      }

      if (e.detail.value.reason == "") {
        wx.showToast({
          title: '请输入退货原因',
          icon: 'none',
          duration: 2000
        })
        return false
      }

      if (that.data.imagesLists.length == 0) {
        wx.showToast({
          title: '请上传图片',
          icon: 'none',
          duration: 2000
        })
        return false
      } 
     
      returnNum = Number(e.detail.value.goodsNum) 
     
    
    }


 
    api.orderRefund1({
      orderCode: that.data.orderCode,
      goodsDetailsId: that.data.goodsDetailsId,
      orderDetailsId: that.data.orderDetailsId,
      returnNum: returnNum == "" ? '0' : returnNum ,
      returnAmount: returnAmount == "" ? '0' : returnAmount,
      remarks :e.detail.value.reason,
      list:that.data.arrImgs,
      type: Number(currentTab)+1
    }).then(res=>{
        if(res.result){
          //动画隐藏
          wx.hideLoading();
          wx.showToast({
            title: '退货申请成功',
            icon: "none",
            duration: 1000,
            success: () => {
              setTimeout(function () {
                wx.navigateBack({
                  delta: 2
                })
              }, 1000)

            }
          })
        }else{
          //动画隐藏
          wx.hideLoading();
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          })
        }
    })
  },
   //提交
  handleSubmit2: function (e) {
    let that = this
    //选择的是退货 退款 
    let currentTab = that.data.currentTab
    //退货数量
    let returnNum = ""
    //退货金额
    let returnAmount = ""

    let myreg = /(^[1-9]\d*$)/;


    wx.showLoading({
      title: '加载中...'
    })

    if (currentTab == 1) {

      if (Number(e.detail.value.priceNum) == "") {
        wx.showToast({
          title: '请输入退款金额',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      //如果输入金额大于总金额 不符合
      if (Number(e.detail.value.priceNum) > Number(that.data.total)) {
        wx.showToast({
          title: '请输入正确金额',
          icon: 'none',
          duration: 2000
        })
        return false
      }

      if (e.detail.value.reason == "") {
        wx.showToast({
          title: '请输入退货原因',
          icon: 'none',
          duration: 2000
        })
        return false
      }

      if (that.data.imagesLists.length == 0) {
        wx.showToast({
          title: '请上传图片',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      returnAmount = Number(e.detail.value.priceNum)


    }

    api.orderRefund1({
      orderCode: that.data.orderCode,
      goodsDetailsId: that.data.goodsDetailsId,
      orderDetailsId: that.data.orderDetailsId,
      returnNum: returnNum == "" ? '0' : returnNum,
      returnAmount: returnAmount == "" ? '0' : returnAmount,
      remarks: e.detail.value.reason,
      list: that.data.arrImgs,
      type: Number(currentTab) + 1
    }).then(res => {
      if (res.result) {
        //动画隐藏
        wx.hideLoading();
        wx.showToast({
          title: '退货申请成功',
          icon: "none",
          duration: 1000,
          success: () => {
            setTimeout(function () {
              wx.navigateBack({
                delta: 2
              })
            }, 1000)

          }
        })
      } else {
        //动画隐藏
        wx.hideLoading();
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000,
        })
      }
    })
  },
   //提交
  handleSubmit3: function (e) {
    let that = this
    //选择的是退货 退款 
    let currentTab = that.data.currentTab
    //退货数量
    let returnNum = ""
    //退货金额
    let returnAmount = ""

    let myreg = /(^[1-9]\d*$)/;


    wx.showLoading({
      title: '加载中...'
    })

    if (currentTab == 2) {
      if (Number(e.detail.value.goodsNum1) == "") {
        wx.showToast({
          title: '请输入退货数量',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      //如果输入的商品数量大于 真实的数量 提示
      if (Number(e.detail.value.goodsNum1) > Number(that.data.amount)) {
        wx.showToast({
          title: '请输入正确的数量',
          icon: 'none',
          duration: 2000
        })
        return false
      }

      if (!myreg.test(e.detail.value.goodsNum1)) {
        wx.showToast({
          title: '请输入正确的数量',
          icon: 'none',
          duration: 2000
        })
        return false
      }


      if (Number(e.detail.value.priceNum1) == "") {
        wx.showToast({
          title: '请输入退货金额',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      //如果输入金额大于总金额 不符合
      if (Number(e.detail.value.priceNum1) > Number(that.data.total)) {
        wx.showToast({
          title: '请输入正确金额',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      if (e.detail.value.reason == "") {
        wx.showToast({
          title: '请输入退货原因',
          icon: 'none',
          duration: 2000
        })
        return false
      }

      if (that.data.imagesLists.length == 0) {
        wx.showToast({
          title: '请上传图片',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      returnAmount = Number(e.detail.value.priceNum1)
      returnNum = Number(e.detail.value.goodsNum1)
    }

    api.orderRefund1({
      orderCode: that.data.orderCode,
      goodsDetailsId: that.data.goodsDetailsId,
      orderDetailsId: that.data.orderDetailsId,
      returnNum: returnNum == "" ? '0' : returnNum,
      returnAmount: returnAmount == "" ? '0' : returnAmount,
      remarks: e.detail.value.reason,
      list: that.data.arrImgs,
      type: Number(currentTab) + 1
    }).then(res => {
      if (res.result) {
        //动画隐藏
        wx.hideLoading();
        wx.showToast({
          title: '退货申请成功',
          icon: "none",
          duration: 1000,
          success: () => {
            setTimeout(function () {
              wx.navigateBack({
                delta: 2
              })
            }, 1000)

          }
        })
      } else {
        //动画隐藏
        wx.hideLoading();
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000,
        })
      }
    })
  }
})